package utils.union_find_helper;

import e_union_find.a_union_find_of_quick_find_type.UnionFind1;
import e_union_find.b_union_find_of_quick_union_type.UnionFind2;
import e_union_find.c_union_find_opt_by_size.UnionFind3;
import e_union_find.d_union_find_opt_by_rank.UnionFind4;
import e_union_find.e_union_find_opt_by_path_compression.UnionFind51;
import e_union_find.e_union_find_opt_by_path_compression.UnionFind52;

import java.util.Random;

public class UnionFindHelper {


    public static void testQuickFindUnionFind1(int size) {
        UnionFind1 u = new UnionFind1(size);
        Random rand = new Random();
        long start, mid, end;
        start = System.currentTimeMillis();
        //union
        for (int i = 0; i < size; i++) {
            u.union(rand.nextInt(size), rand.nextInt(size));
        }
        mid = System.currentTimeMillis();
        //find
        for (int i = 0; i < size; i++) {
            u.isConnected(rand.nextInt(size), rand.nextInt(size));
        }
        end = System.currentTimeMillis();
        System.out.println("UF1  " + size + " ops " + (end - start) + "ms. \tunion: " + (mid - start) + "ms." + " \tfind: " + (end - mid) + "ms.");
    }

    public static void testQuickUnionUnionFind2(int size) {
        UnionFind2 u = new UnionFind2(size);
        Random rand = new Random();
        long start, mid, end;
        start = System.currentTimeMillis();
        //union
        for (int i = 0; i < size; i++) {
            u.union(rand.nextInt(size), rand.nextInt(size));
        }
        mid = System.currentTimeMillis();
        //find
        for (int i = 0; i < size; i++) {
            u.isConnected(rand.nextInt(size), rand.nextInt(size));
        }
        end = System.currentTimeMillis();
        System.out.println("UF2  " + size + " ops " + (end - start) + "ms. \tunion: " + (mid - start) + "ms." + " \tfind: " + (end - mid) + "ms.");
    }

    public static void testUnionFindOptBySize3(int size) {
        UnionFind3 u = new UnionFind3(size);
        Random rand = new Random();
        long start, mid, end;
        start = System.currentTimeMillis();
        //union
        for (int i = 0; i < size; i++) {
            u.union(rand.nextInt(size), rand.nextInt(size));
        }
        mid = System.currentTimeMillis();
        //find
        for (int i = 0; i < size; i++) {
            u.isConnected(rand.nextInt(size), rand.nextInt(size));
        }
        end = System.currentTimeMillis();
        System.out.println("UF3  " + size + " ops " + (end - start) + "ms. \tunion: " + (mid - start) + "ms." + " \tfind: " + (end - mid) + "ms.");
    }


    public static void testUnionFindOptByRank4(int size) {
        UnionFind4 u = new UnionFind4(size);
        Random rand = new Random();
        long start, mid, end;
        start = System.currentTimeMillis();
        //union
        for (int i = 0; i < size; i++) {
            u.union(rand.nextInt(size), rand.nextInt(size));
        }
        mid = System.currentTimeMillis();
        //find
        for (int i = 0; i < size; i++) {
            u.isConnected(rand.nextInt(size), rand.nextInt(size));
        }
        end = System.currentTimeMillis();
        System.out.println("UF4  " + size + " ops " + (end - start) + "ms. \tunion: " + (mid - start) + "ms." + " \tfind: " + (end - mid) + "ms.");
    }

    public static void testUnionFindOptByPathCompression51(int size) {
        UnionFind51 u = new UnionFind51(size);
        Random rand = new Random();
        long start, mid, end;
        start = System.currentTimeMillis();
        //union
        for (int i = 0; i < size; i++) {
            u.union(rand.nextInt(size), rand.nextInt(size));
        }
        mid = System.currentTimeMillis();
        //find
        for (int i = 0; i < size; i++) {
            u.isConnected(rand.nextInt(size), rand.nextInt(size));
        }
        end = System.currentTimeMillis();
        System.out.println("UF51  " + size + " ops " + (end - start) + "ms. \tunion: " + (mid - start) + "ms." + " \tfind: " + (end - mid) + "ms.");
    }

    public static void testUnionFindOptByPathCompression52(int size) {
        UnionFind52 u = new UnionFind52(size);
        Random rand = new Random();
        long start, mid, end;
        start = System.currentTimeMillis();
        //union
        for (int i = 0; i < size; i++) {
            u.union(rand.nextInt(size), rand.nextInt(size));
        }
        mid = System.currentTimeMillis();
        //find
        for (int i = 0; i < size; i++) {
            u.isConnected(rand.nextInt(size), rand.nextInt(size));
        }
        end = System.currentTimeMillis();
        System.out.println("UF52  " + size + " ops " + (end - start) + "ms. \tunion: " + (mid - start) + "ms." + " \tfind: " + (end - mid) + "ms.");
    }
}
